package com.zzx.business.controller;

import com.zzx.common.annotation.Log;
import com.zzx.common.core.controller.BaseController;
import com.zzx.common.core.domain.AjaxResult;
import com.zzx.common.enums.BusinessType;
import com.zzx.common.utils.poi.ExcelUtil;
import com.zzx.business.vo.TbBookVo;
import com.zzx.business.service.ITbBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.zzx.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 标准图书Controller
 *
 * @author Zzx
 * @date 2025-05-30
 */
@Api(tags = "标准图书Controller")
@RestController
@RequestMapping("/business/book")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TbBookController extends BaseController {

    private final ITbBookService tbBookService;

    @ApiOperation("查询标准图书列表")
    @PreAuthorize("@ss.hasPermi('business:book:list')")
    @GetMapping("/list")
    public TableDataInfo<TbBookVo> list(TbBookVo entity) {
        return tbBookService.queryList(entity);
    }

    @ApiOperation("查询标准图书所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TbBookVo entity) {
        return AjaxResult.success("查询成功", tbBookService.queryAll(entity));
    }

    @ApiOperation("导出标准图书列表")
    @PreAuthorize("@ss.hasPermi('business:book:export')")
    @Log(title = "标准图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBookVo entity) {
        List<TbBookVo> list = tbBookService.queryAll(entity);
        ExcelUtil<TbBookVo> util = new ExcelUtil<>(TbBookVo.class);
        util.exportExcel(response, list, "标准图书数据");
    }

    @ApiOperation("获取标准图书详细信息")
    @PreAuthorize("@ss.hasPermi('business:book:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tbBookService.queryById(id));
    }

    @ApiOperation("新增标准图书")
    @PreAuthorize("@ss.hasPermi('business:book:add')")
    @Log(title = "标准图书", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TbBookVo entity) {
        return toAjax(tbBookService.save(entity));
    }

    @ApiOperation("修改标准图书")
    @PreAuthorize("@ss.hasPermi('business:book:edit')")
    @Log(title = "标准图书", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TbBookVo entity) {
        return toAjax(tbBookService.updateById(entity));
    }

    @ApiOperation("删除标准图书")
    @PreAuthorize("@ss.hasPermi('business:book:remove')")
    @Log(title = "标准图书", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbBookService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
